﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using DevExpress.XtraTreeList.Nodes;
using System.Reflection;
using Common;
using Model;

namespace CodeFactory
{
    public partial class PropertyEventSelector : Form
    {
        #region 私有字段
        private string ControlFullName;
        /// <summary>
        /// 父窗体实例
        /// </summary>
        private CustomPropertyGrid cpg;
        private enumPropertyType enumPT;
        #endregion


        #region 初始化
        public PropertyEventSelector()
        {
            InitializeComponent();
        }

        public PropertyEventSelector(string _ControlFullName,enumPropertyType _enumPT, CustomPropertyGrid _cpg)
        {
            InitializeComponent();
            ControlFullName = _ControlFullName;
            cpg = _cpg;
            enumPT = _enumPT;
        }

        private void PropertySelector_Load(object sender, EventArgs e)
        {
            SetTreeColumnVisible(enumPT);
            Dictionary<string,object> dic = GetPropertysOfControl(enumPT);
            BuildPropertyTree(dic, enumPT);
        }
        #endregion


        #region 私有方法

        /// <summary>
        /// 根据种类返回属性和事件的列表
        /// </summary>
        /// <param name="enumPBC"></param>
        /// <returns></returns>
        private Dictionary<string,object> GetPropertysOfControl(enumPropertyType _enumPT)
        {
            try
            {
               
                Type t = Type.GetType(ControlFullName);
                Dictionary<string, object> dic = new Dictionary<string, object>();
                switch (_enumPT)
                {
                    case enumPropertyType.Property:
                        foreach (PropertyInfo pi in t.GetProperties())
                        {
                            if (!dic.ContainsKey(pi.Name))
                            {
                                dic.Add(pi.Name, String.Format("{0}-{1}", pi.PropertyType.Name, pi.PropertyType.FullName));
                            }
                        }
                        return dic;
                    case enumPropertyType.Event:
                        foreach (EventInfo ei in t.GetEvents())
                        {
                            if (!dic.ContainsKey(ei.Name))
                            {
                                dic.Add(ei.Name, ei.EventHandlerType);
                            }
                        }
                        return dic;
                    default:
                        return dic;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 设置列的显示与否
        /// </summary>
        /// <param name="enumPBC"></param>
        private void SetTreeColumnVisible(enumPropertyType _enumPT)
        {
            switch (_enumPT)
            {
                case enumPropertyType.Property:
                     treeColumnEventHandleType.Visible = false;
                     treeColumnPropertyType.Visible = true;
                     treeColumnPropertyTypeFullName.Visible = true;
                     break;
                case enumPropertyType.Event:
                     treeColumnEventHandleType.Visible = true;
                     treeColumnPropertyType.Visible = false;
                     treeColumnPropertyTypeFullName.Visible = false;
                     break;
            }
        }

        /// <summary>
        /// DictionaryEntry
        /// key=PropertyName
        /// value=PropertyTypeName-PropertyTypeFullName
        /// </summary>
        private void BuildPropertyTree(Dictionary<string, object> dic, enumPropertyType _enumPT)
        {
            switch(_enumPT)
            {
                case enumPropertyType.Property:
                      foreach (KeyValuePair<string,object> de in dic)
                        {
                           TreeListNode tln = treePropertySelector.AppendNode(null, null);
                           tln.SetValue(0, de.Key);
                           tln.SetValue(1, de.Value.ToString().Split('-')[0]);
                           tln.SetValue(2, de.Value.ToString().Split('-')[1]);
                        }
                       break;
                case enumPropertyType.Event:
                       foreach (KeyValuePair<string, object> de in dic)
                       {
                           TreeListNode tln = treePropertySelector.AppendNode(null, null);
                           tln.SetValue(0, de.Key);
                           tln.SetValue(3, de.Value);
                       }
                       break;
            }
        }

        
        /// <summary>
        /// 获取需要添加的属性列表
        /// </summary>
        /// <param name="enumPBC">属性的种类（事件，属性，方法等）</param>
        /// <returns>需要添加的属性列表</returns>
        private List<eProperty> GetPropertysFromSelected(enumPropertyType _enumPT)
        {
            List<eProperty> list = null; 
            eProperty ep = null;
            try
            {   list=new List<eProperty>();
                foreach (TreeListNode tln in treePropertySelector.Nodes)
                {
                    if (tln.Checked)
                    {
                        ep = new eProperty();
                        ep.Name = tln.GetValue(0) == null ? "" : tln.GetValue(0).ToString();

                        switch (_enumPT)
                        {
                            case enumPropertyType.Property:
                                ep.PropertyType = enumPropertyType.Property;
                                ep.ValueType = tln.GetValue(1) == null ? "" : tln.GetValue(1).ToString();
                                ep.ValueTypeFullName = tln.GetValue(2) == null ? "" : tln.GetValue(2).ToString();
                                break;
                            case enumPropertyType.Event:
                                ep.PropertyType = enumPropertyType.Event;
                                ep.ValueType = "String";
                                ep.ValueTypeFullName = "Event";
                                ep.EventArgs = tln.GetValue(3) == null ? "" : tln.GetValue(3).ToString();
                                break;
                        }
                        list.Add(ep);
                    }
                }
                return list;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }



        #endregion


        #region 按钮事件

        private void btnOK_Click(object sender, EventArgs e)
        {
            List<eProperty> list = GetPropertysFromSelected(enumPT);
            cpg.AddPropertysToTree(list,enumPT);
            this.Close();
        }


        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }


        private void btnSelectAll_Click(object sender, EventArgs e)
        {
            foreach (TreeListNode tln in treePropertySelector.Nodes)
            {
                tln.CheckState = CheckState.Checked;
                tln.Checked = true;
            }
        }


        private void btnSelectNone_Click(object sender, EventArgs e)
        {
            foreach (TreeListNode tln in treePropertySelector.Nodes)
            {
                tln.CheckState = CheckState.Unchecked;
                tln.Checked = false;
            }
        }

        
        private void btnSelectChecked_Click(object sender, EventArgs e)
        {
            foreach (TreeListNode tln in treePropertySelector.Nodes)
            {
                if (tln.Selected)
                {
                    tln.CheckState = CheckState.Checked;
                    tln.Checked = true;
                }
            }
        }
        #endregion

       




     

    }
}
